<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="大计划id" prop="schedulesId">
        <el-input
          v-model="queryParams.schedulesId"
          placeholder="请输入大计划id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务的标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入任务的标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务的截止日期" prop="dueDate">
        <el-date-picker clearable
          v-model="queryParams.dueDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择任务的截止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="任务的创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择任务的创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="任务的更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择任务的更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pom:tasks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pom:tasks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pom:tasks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pom:tasks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键，唯一标识每个任务" align="center" prop="id" />
      <el-table-column label="大计划id" align="center" prop="schedulesId" />
      <el-table-column label="任务类型：基础知识、进阶篇" align="center" prop="type" />
      <el-table-column label="任务的标题" align="center" prop="title" />
      <el-table-column label="任务的描述" align="center" prop="description" />
      <el-table-column label="任务的状态" align="center" prop="status" />
      <el-table-column label="任务的优先级" align="center" prop="priority" />
      <el-table-column label="任务的截止日期" align="center" prop="dueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务的创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务的更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pom:tasks:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pom:tasks:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改tasks对话框 -->
    <el-dialog :title="title" :visible.sync="open" fullscreen append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="大计划id" prop="schedulesId">
          <el-input v-model="form.schedulesId" placeholder="请输入大计划id" />
        </el-form-item>
        <el-form-item label="任务的标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入任务的标题" />
        </el-form-item>
        <el-form-item label="任务的描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="任务的截止日期" prop="dueDate">
          <el-date-picker clearable
            v-model="form.dueDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择任务的截止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务的创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择任务的创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务的更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择任务的更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标记" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">review信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPomEbbinghausReview">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePomEbbinghausReview">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="pomEbbinghausReviewList" :row-class-name="rowPomEbbinghausReviewIndex" @selection-change="handlePomEbbinghausReviewSelectionChange" ref="pomEbbinghausReview">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="类型：基础知识、进阶篇" prop="type" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.type" placeholder="请选择类型：基础知识、进阶篇">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="用户ID，标识哪个用户的学习计划" prop="userId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userId" placeholder="请输入用户ID，标识哪个用户的学习计划" />
            </template>
          </el-table-column>
          <el-table-column label="初次学习的时间" prop="initialStudyTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.initialStudyTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择初次学习的时间" />
            </template>
          </el-table-column>
          <el-table-column label="第一次复习的时间" prop="reviewTime1" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.reviewTime1" type="date" value-format="yyyy-MM-dd" placeholder="请选择第一次复习的时间" />
            </template>
          </el-table-column>
          <el-table-column label="第二次复习的时间" prop="reviewTime2" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.reviewTime2" type="date" value-format="yyyy-MM-dd" placeholder="请选择第二次复习的时间" />
            </template>
          </el-table-column>
          <el-table-column label="第三次复习的时间" prop="reviewTime3" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.reviewTime3" type="date" value-format="yyyy-MM-dd" placeholder="请选择第三次复习的时间" />
            </template>
          </el-table-column>
          <el-table-column label="第四次复习的时间" prop="reviewTime4" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.reviewTime4" type="date" value-format="yyyy-MM-dd" placeholder="请选择第四次复习的时间" />
            </template>
          </el-table-column>
          <el-table-column label="第五次复习的时间" prop="reviewTime5" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.reviewTime5" type="date" value-format="yyyy-MM-dd" placeholder="请选择第五次复习的时间" />
            </template>
          </el-table-column>
          <el-table-column label="复习计划的创建时间" prop="createdAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createdAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择复习计划的创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="复习计划的更新时间" prop="updatedAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.updatedAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择复习计划的更新时间" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTasks, getTasks, delTasks, addTasks, updateTasks } from "@/api/pom/tasks";

export default {
  name: "Tasks",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPomEbbinghausReview: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // tasks表格数据
      tasksList: [],
      // review表格数据
      pomEbbinghausReviewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schedulesId: null,
        type: null,
        title: null,
        description: null,
        status: null,
        priority: null,
        dueDate: null,
        createdAt: null,
        updatedAt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询tasks列表 */
    getList() {
      this.loading = true;
      listTasks(this.queryParams).then(response => {
        this.tasksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        schedulesId: null,
        type: null,
        title: null,
        description: null,
        status: null,
        priority: null,
        dueDate: null,
        createdAt: null,
        updatedAt: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null
      };
      this.pomEbbinghausReviewList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加tasks";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTasks(id).then(response => {
        this.form = response.data;
        this.pomEbbinghausReviewList = response.data.pomEbbinghausReviewList;
        this.open = true;
        this.title = "修改tasks";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.pomEbbinghausReviewList = this.pomEbbinghausReviewList;
          if (this.form.id != null) {
            updateTasks(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTasks(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除tasks编号为"' + ids + '"的数据项？').then(function() {
        return delTasks(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** review序号 */
    rowPomEbbinghausReviewIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** review添加按钮操作 */
    handleAddPomEbbinghausReview() {
      let obj = {};
      obj.type = "";
      obj.userId = "";
      obj.content = "";
      obj.initialStudyTime = "";
      obj.reviewTime1 = "";
      obj.reviewTime2 = "";
      obj.reviewTime3 = "";
      obj.reviewTime4 = "";
      obj.reviewTime5 = "";
      obj.createdAt = "";
      obj.updatedAt = "";
      obj.remark = "";
      this.pomEbbinghausReviewList.push(obj);
    },
    /** review删除按钮操作 */
    handleDeletePomEbbinghausReview() {
      if (this.checkedPomEbbinghausReview.length == 0) {
        this.$modal.msgError("请先选择要删除的review数据");
      } else {
        const pomEbbinghausReviewList = this.pomEbbinghausReviewList;
        const checkedPomEbbinghausReview = this.checkedPomEbbinghausReview;
        this.pomEbbinghausReviewList = pomEbbinghausReviewList.filter(function(item) {
          return checkedPomEbbinghausReview.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePomEbbinghausReviewSelectionChange(selection) {
      this.checkedPomEbbinghausReview = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pom/tasks/export', {
        ...this.queryParams
      }, `tasks_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
